package io;

import java.io.*;
import java.util.Scanner;

/**
 * 实现简易记事本工具
 * 程序启动后,要求用户输入一个文件名,然后对该文件写操作
 * 之后用户输入的每一行字符串都能按行写入到该文件中
 * 当用户单独输入exit时程序退出
 */
public class Test2 {
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件名:");
        String path = scanner.nextLine();
        FileOutputStream fos =
                new FileOutputStream(path+".txt",true);
        OutputStreamWriter osw =
                new OutputStreamWriter(fos,"UTF-8");
        BufferedWriter bw =
                new BufferedWriter(osw);
        /*
            在流连接中创建PrintWrite时可以在传入一个boolean型的参数
            如果这个值为true,则当前PrintWrite就具有了自动刷新功能
            即:当我们每次调用println方法后会自动调用flush
         */
        PrintWriter pw =
                new PrintWriter(bw);
        while (true){
            System.out.println("请继续输入文字:");
            String str = scanner.nextLine();
            if ("exit".equalsIgnoreCase(str)){
                System.out.println("程序退出.");
                break;
            }
            pw.println(str);
        }
        pw.close();
    }
    public static void ssr() throws FileNotFoundException, UnsupportedEncodingException {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入文件名:");
        String fileName = s.nextLine();
        FileOutputStream fos = new FileOutputStream(fileName+".txt",true);
        OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        while (true){
            System.out.println("请继续输入:");
            String mais = s.nextLine();
            if (("exit".equalsIgnoreCase(mais))){
                System.out.println("退出");
                break;
            }
            pw.println(mais);
        }
        pw.close();
    }
}
